<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="DictTypeModal">
  // defineOptions({name:"DictTypeModal"})
  import { computed, ref, unref } from "vue";
  import { BasicModal, useModalInner } from "@/components/Modal";
  import { BasicForm, useForm } from "@/components/Form";
  import { dictForm } from "./dict.data";
  import { AddDictTypeItem, UpdateDictTypeItem } from "@/api/system/model/DictModel";
  import { AddDictType, UpdateDictType } from "@/api/system/dict";
  import { notification } from "ant-design-vue";
  import { useI18n } from '@/hooks/web/useI18n';
  import { useDictStore } from "@/store/modules/dict";
  const { t }  = useI18n();
  //定义emit
  const emit = defineEmits(["success", "register"]);
  const isUpdate = ref(true); //是否更新
  let update: any; //更新数据
  const getTitle = computed(() => (!unref(isUpdate) ? '新增字典类型' : '编辑字典类型'));
  const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: dictForm,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    update = data.record
    if (unref(isUpdate)) {
      await setFieldsValue({
        ...data.record,
      });
    }
  });
  async function handleSubmit() {
    const values = await validate();
    try {
      setModalProps({ confirmLoading: true });
      console.log(values)
      console.log(update)
      if(unref(isUpdate)) {
        //更新
        let updateData:UpdateDictTypeItem = {
          dictId: update.dictId,
          dictName: values.dictName,
          dictType: values.dictType,
          status: values.status,
          remark: values.remark
        }
        const res =  await UpdateDictType(updateData);
        if(res.code == 200) {
          notification.success({
            message: t('sys.update.success'),
            description: t('sys.update.success'),
            duration: 3,
          });
          useDictStore().delDict(values.dictType);
          closeModal();
          emit('success');
        }else{
          notification.error({
            message: t('sys.update.error'),
            description: res.msg,
            duration: 3,
          });
        }
      }else{
        //新增
        let addData:AddDictTypeItem = {
          dictName: values.dictName,
          dictType: values.dictType,
          status: values.status,
          remark: values.remark
        }
        const res =  await AddDictType(addData)
        if(res.code == 200){
          notification.success({
            message: t('sys.add.addsuccess'),
            description: t('sys.add.addsuccess'),
            duration: 3,
          });
          closeModal();
          emit('success');
        }else{
          notification.error({
            message: t('sys.add.addfail'),
            description: res.msg,
            duration: 3,
          });
        }
        setModalProps({ confirmLoading: false });
      }
    } catch (error) {
      console.log(error)
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
<style lang="scss" scoped>

</style>
